package com.javasm.dao.MisMannageDao.impl;



import com.javasm.dao.MisMannageDao.TypeDao;
import com.javasm.entity.MisMannageEntity.TypeInfo;
import com.javasm.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/24 10:09
 * @version:0.1
 * @since:jdk11
 */
public class TypeDaoImpl implements TypeDao {

    @Override
    public List<TypeInfo> getSecondSeriesName() {

       Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;

        StringBuilder sb = new StringBuilder();
        sb.append("select tt.type_id,tt.type_name from tb_type tt where tt.type_parent_id<>0");

        List<TypeInfo> typeList = new ArrayList<TypeInfo>();
        try {
            ps = conn.prepareStatement(sb.toString());
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer typeId = rs.getInt("type_id");
                String typeName = rs.getString("type_name");

                TypeInfo typeInfo = new TypeInfo();
                typeInfo.setTypeId(typeId);
                typeInfo.setTypeName(typeName);
                typeList.add(typeInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.closeConn(conn, ps,null, rs);
        }
        return typeList;

    }

    @Override
    public String getTypeName(Integer typeId) {
       Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;

        StringBuilder sb = new StringBuilder();
        sb.append("select tt.type_name from tb_type tt where tt.type_id=?");

        String typeName = null;
        try {
            ps = conn.prepareStatement(sb.toString());
            ps.setInt(1,typeId);
            rs = ps.executeQuery();
            while (rs.next()) {
                typeName = rs.getString("type_name");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeConn(conn, ps,null, rs);
        }
        return typeName;
    }

    @Override
    public Integer getTypeId(String typeName) {
       Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;

        StringBuilder sb = new StringBuilder();
        sb.append("select tt.type_id from tb_type tt where tt.type_name=?");

        Integer typeId = 0;
        try {
            ps = conn.prepareStatement(sb.toString());
            ps.setString(1,typeName);
            rs = ps.executeQuery();
            while (rs.next()) {
                typeId = rs.getInt("type_id");

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeConn(conn, ps,null, rs);
        }
        return typeId;
    }
}
